package com.css.dzj.dzjap.modules.info.controller;

import com.alibaba.fastjson.JSON;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.audit.service.SysAuditService;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.construnit.entity.ConstructionUnitEntity;
import com.css.dzj.dzjap.modules.construnit.service.ConstrunitService;
import com.css.dzj.dzjap.modules.info.service.ConstrService;
import com.css.dzj.dzjap.modules.info.vo.EvalInfoVo;
import com.querydsl.core.QueryResults;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 建设单位
 *
 * @author liangxingguo
 */
@RestController
@RequestMapping("/constr")
public class ConstrController {

    @Autowired
    ConstrService constrService;
    @Autowired
    ConstrunitService construnitService;
    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;
    /***
     * 分页条件查询
     * @param pageNo
     * @param pageSize
     * @param entName
     * @param createPerson
     * @return
     */
    @GetMapping("/listpage")
    public R getPageContr(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                          @RequestParam(name = "entName", required = false) String entName,
                          @RequestParam(name = "uniscid", required = false) String uniscid,
                          @RequestParam(name = "startTime", required = false) String startTime,
                          @RequestParam(name = "endTime", required = false) String endTime,
                          @RequestParam(name = "createPerson", required = false) String createPerson, HttpServletRequest request) {
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults page = constrService.getPage(pageRequest, entName, uniscid, startTime, endTime, createPerson, request);
            s = JSON.toJSONString(page);
            sysAuditService.saveSuccessAudit("查询成功", Constant.CONSTR, Constant.PT, Constant.PAGE, s, request);
            return R.ok().put("page", page);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询失败", Constant.CONSTR, Constant.PT, Constant.PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 分页查询建设单位
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/listcontr")
    public R getContr(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                      @RequestParam(name = "enterpriseName", required = false) String enterpriseName,
                      @RequestParam(name = "enterpriseIdNumber", required = false) String enterpriseIdNumber) {
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults page = constrService.getPageConstr(pageRequest, enterpriseName, enterpriseIdNumber);
            List<ConstructionUnitEntity> list = page.getResults();
            List<EvalInfoVo> evalInfoVoList = new ArrayList<>();
            for (ConstructionUnitEntity constructionUnitEntity : list) {
                EvalInfoVo evalInfoVo = new EvalInfoVo();
                evalInfoVo.setEnterpriseName(constructionUnitEntity.getEntName());
                evalInfoVo.setEnterpriseIdNumber(constructionUnitEntity.getUniscid());
                evalInfoVo.setEnterpriseIdType(constructionUnitEntity.getEntType());
                evalInfoVo.setLegalMan(constructionUnitEntity.getLegalPerson());
                evalInfoVo.setRecordUnitUser(constructionUnitEntity.getLinkMan());
                evalInfoVo.setRecordUnitPhone(constructionUnitEntity.getLinkTel());
                evalInfoVoList.add(evalInfoVo);
            }
            QueryResults queryResults = new QueryResults(evalInfoVoList, page.getTotal(), page.getOffset(), page.getTotal());
            s = JSON.toJSONString(queryResults);
            sysAuditService.saveSuccessAudit("查询建设单位信息成功", Constant.CONSTR, Constant.PT, Constant.PAGE, s, request);
            return R.ok().put("data", queryResults);
        }catch ( Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询建设单位信息失败", Constant.CONSTR, Constant.PT, Constant.PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }


    /***
     * 逻辑删除
     * @param id
     * @return
     */
    @PostMapping("/delConstr/{id}")
    public R delConstr(@PathVariable String id) {
        String s = "";
        try{
            boolean flag = constrService.delConstr(id);
            if (flag) {
                s  = JSON.toJSONString(id);
                sysAuditService.saveSuccessAudit("删除成功", Constant.CONSTR, Constant.PT, Constant.DEL, s, request);
                return R.ok("删除成功");
            }
            return R.error("删除失败");
    }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("删除失败", Constant.CONSTR, Constant.YZ, Constant.DEL, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /***
     * 更新建设单位
     * @param constructionUnitEntity
     * @return
     */
    @PostMapping("/update")
    public R updateConstr(@RequestBody ConstructionUnitEntity constructionUnitEntity) {
        String s = "";
        try {
            ConstructionUnitEntity constructionEntity = constrService.findById(constructionUnitEntity.getId());
            BeanUtil.copyBeanNotEmptyToBean(constructionUnitEntity, constructionEntity);
            constructionEntity.setUdateTime(new Date());
            constrService.update(constructionEntity);
            s = JSON.toJSONString(constructionEntity);
            sysAuditService.saveSuccessAudit("修改成功", Constant.CONSTR, Constant.PT, Constant.UPDATE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("公示通报修改失败", Constant.CONSTR, Constant.YZ, Constant.UPDATE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /***
     * 根据ID查询建设单位
     * @param id
     * @return
     */
    @PostMapping("/findbyid/{id}")
    public R findById(@PathVariable String id) {
        String s = "";
        try{
        if (!StringUtils.isEmpty(id)) {
            ConstructionUnitEntity constructionUnitEntity = constrService.findById(id);
            s = JSON.toJSONString(constructionUnitEntity);
            sysAuditService.saveSuccessAudit("查询成功", Constant.CONSTR, Constant.PT, Constant.SELECT, s, request);
            return R.ok().put("constructionUnitEntity", constructionUnitEntity);
        } else {
            return R.error("id为空");
        }
        }catch ( Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询失败", Constant.CONSTR, Constant.YZ, Constant.SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /***
     * 添加建筑单位
     * @param constructionUnitEntity
     * @return
     */
    @PostMapping("/saveconstr")
    public R save(@RequestBody ConstructionUnitEntity constructionUnitEntity) {
        String s = "";
        try {
            // 判断企业名称是否已存在
            boolean isExistUserName = construnitService.isExistUserName(constructionUnitEntity.getUserName());
            if (isExistUserName) {
                return R.error("企业名称已存在!");
            }
            // 判断统一社会信用代码是否已存在
            boolean isExistEntCode = construnitService.isExistEntCode(constructionUnitEntity.getUniscid());
            if (isExistEntCode) {
                return R.error("统一社会信用代码已存在!");
            }

            construnitService.regist(constructionUnitEntity);
            s = JSON.toJSONString(constructionUnitEntity);
            sysAuditService.saveSuccessAudit("查询成功", Constant.CONSTR, Constant.PT, Constant.ADD, s, request);
            return R.ok("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("修改投诉举报失败", Constant.CONSTR, Constant.PT, Constant.ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /***
     * 获取建设单位
     * @return
     */
    @GetMapping("/countconstr")
    public R countconstr() {
        HashMap count = constrService.getCount();
        return R.ok().put("count", count);
    }

    /***
     * 获取建设单位根据年度
     * @return
     */
    @GetMapping("/countconstryear")
    public R countconstrByYear(String year) {
        HashMap count = constrService.getCountByYear(year);
        return R.ok().put("count", count);
    }

   /* *//**
     * 获取list
     *//*
    @GetMapping("/getPageLH")
    public List getPageLH(@RequestParam(name = "unitName", required = false) String unitName,
                          @RequestParam(name = "uniscid", required = false) String uniscid) {
        List<ConstructionUnitEntity> page = constrService.getPageLH( unitName, uniscid);
        return page;
    }*/
}
